/*******************************************************************************
Name: migration
*******************************************************************************/


clear

capture log close
log using  "${logdir}9-migration.log", replace


use "${datadir}ls_80_panel.dta"


gen metarea5yr = 0
replace metarea5yr = . if migrate5d == 0
replace metarea5yr = metarea if migrate5d == 10 | migrate5d == 21
replace metarea5yr = floor(migmet5/10) if migrate5d > 21

rename metarea metarea_post
rename metarea5yr metarea

drop if metarea == . | metarea == 0



gen idle5yr = (coll5yr == 1 & work5yr == 1)

egen count_ls = count(year), by(metarea race sex cohort_cat educ_cat)
egen weight_cell = sum(perwt), by(year metarea race sex cohort_cat educ_cat)

gen active = 1 - idle
gen active5yr = 1 - idle5yr

gen migrate = (metarea != metarea_post)

gen active5yrXstay = active5yr*(!migrate)

preserve

rename count_ls count_ls5yr
rename weight_cell weight_cell5yr

collapse (mean) idle5yr active5yr count_ls5yr weight_cell5yr [aw = perwt], by(year metarea race sex cohort_cat educ_cat)

save "${datadir}synthmig5yr_temp.dta", replace

restore

collapse (mean) migrate idle idle5yr active active5yr active5yrXstay weight_cell count_ls [aw = perwt], by(year metarea race sex cohort_cat educ_cat)


merge year metarea race sex cohort_cat educ_cat using "${datadir}migration_temp.dta", sort uniqusing

tab _merge

drop if _merge == 2

drop _merge

gen black = (race == 2)


gen idle_fd = idle - idle5yr
gen ln_idle_fd = ln(idle) - ln(idle5yr)

replace active5yrXstay = active5yrXstay/(1 - migrate)

gen active5yr_bias = active5yrXstay - active5yr
gen ln_active5yr_bias = ln(active5yrXstay) - ln(active5yr)

gen ln_active5yr = ln(active5yr)

gen active_1d = active - active5yr
gen ln_active_1d = ln(active) - ln(active5yr)


gen blackXactive5yr = black*(active5yr)
gen blackXln_active5yr = black*(ln_active5yr)

gen active5yr_sq = active5yr^2
gen ln_active5yr_sq = ln_active5yr^2

gen blackXactive5yr_sq = blackXactive5yr^2
gen blackXln_active5yr_sq = blackXln_active5yr^2

replace ln_fcc_1d = 0.5*ln_fcc_1d
replace blackXln_fcc_1d = 0.5*blackXln_fcc_1d

/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
*TABLE A8: JOB SUBURBANIZATION, EMPLOYMENT, AND MIGRATION, 1975-1980
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/


*OUTCOME: ACTIVE
areg ln_active_1d blackXln_fcc_1d ln_fcc_1d [aw = weight_cell] if count > 25, absorb(group) cluster(metarea)
	
areg ln_active_1d blackXln_fcc_1d blackXln_active5yr blackXln_active5yr_sq ln_fcc_1d ///
	ln_active5yr ln_active5yr_sq [aw = weight_cell] if count > 25, absorb(group) cluster(metarea)

*OUTCOME: MIGRATION
areg migrate blackXln_fcc_1d ln_fcc_1d [aw = weight_cell] if count > 25, absorb(group) cluster(metarea)
	
areg migrate blackXln_fcc_1d blackXln_active5yr blackXln_active5yr_sq ln_fcc_1d ///
	ln_active5yr ln_active5yr_sq [aw = weight_cell] if count > 25, absorb(group) cluster(metarea)

*OUTCOME: BIAS
areg ln_active5yr_bias blackXln_fcc_1d ln_fcc_1d [aw = weight_cell] if count > 25, absorb(group) cluster(metarea)
	
areg ln_active5yr_bias blackXln_fcc_1d blackXln_active5yr blackXln_active5yr_sq ln_fcc_1d ///
	ln_active5yr ln_active5yr_sq [aw = weight_cell] if count > 25, absorb(group) cluster(metarea)

gen sample = e(sample)

keep year metarea race sex cohort_cat educ_cat sample
	
save "${datadir}sample_temp.dta", replace

*synthetic panel
clear
	
use "${datadir}ls_80_panel.dta"

egen count_ls = count(year), by(metarea race sex cohort_cat educ_cat)
egen weight_cell = sum(perwt), by(year metarea race sex cohort_cat educ_cat)

gen active = 1 - idle

collapse (mean) idle active weight_cell count_ls [aw = perwt], by(year metarea race sex cohort_cat educ_cat)

merge year metarea race sex cohort_cat educ_cat using "${datadir}synthmig5yr_temp.dta", sort

keep if _merge == 3
drop _merge

merge year metarea race sex cohort_cat educ_cat using "${datadir}migration_temp.dta", sort uniqusing

tab _merge
drop if _merge == 2
drop _merge

merge year metarea race sex cohort_cat educ_cat using "${datadir}sample_temp.dta", sort uniqusing
capture rm "${datadir}sample_temp.dta"

tab _merge
drop if _merge == 2
drop _merge

gen black = (race == 2)


gen idle_fd = idle - idle5yr
gen ln_idle_fd = ln(idle) - ln(idle5yr)

gen ln_active5yr = ln(active5yr)

gen active_1d = active - active5yr
gen ln_active_1d = ln(active) - ln(active5yr)


gen blackXactive5yr = black*(active5yr)
gen blackXln_active5yr = black*(ln_active5yr)

gen active5yr_sq = active5yr^2
gen ln_active5yr_sq = ln_active5yr^2

gen blackXactive5yr_sq = blackXactive5yr^2
gen blackXln_active5yr_sq = blackXln_active5yr^2

replace ln_fcc_1d = 0.5*ln_fcc_1d
replace blackXln_fcc_1d = 0.5*blackXln_fcc_1d


*OUTCOME: ACTIVE
areg ln_active_1d blackXln_fcc_1d ln_fcc_1d [aw = weight_cell] if count_ls > 25 & sample, absorb(group) cluster(metarea)
	
areg ln_active_1d blackXln_fcc_1d blackXln_active5yr blackXln_active5yr_sq ln_fcc_1d ///
	ln_active5yr ln_active5yr_sq [aw = weight_cell] if count_ls > 25 & sample, absorb(group) cluster(metarea)

log close
